package com.lk.servlet;

import com.lk.entity.User;
import com.lk.service.UserService;
import com.lk.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/Add")
public class UserAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username= request.getParameter("username");
        String password= request.getParameter("password");
        User user=new User(username,password);

        UserService userService=new UserServiceImpl();
        int n= userService.addUser(user);

        if (n==1){
            request.setAttribute("user",user);
            request.getRequestDispatcher("userList.jsp").forward(request,response);

        } else if (n==0) {
            request.setAttribute("user","");
        }
        request.getRequestDispatcher("userAdd.html").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
